#include "NetworkSession.hh"
#include <boost/bind.hpp>

NetworkSession::NetworkSession(boost::asio::io_service & service, 
			       boost::function<void(NetworkSession &, void *, size_t)> callbackReceive) : mCallbackReceive(callbackReceive),
													  mSocket(service)
{
  ;
}
  
NetworkSession::~NetworkSession(void)
{
  ;
}
 
void					NetworkSession::handleReceive(const boost::system::error_code & error,
								      size_t size)
{
  if (mCallbackReceive != NULL)
    {
      if (error)
	mCallbackReceive(*this, NULL, 0);
      else
	mCallbackReceive(*this, (void *)mInputData, size);
    }
  if (!error)
    this->receive();
}

void					NetworkSession::receive()
{
  this->mSocket.async_read_some(
			   boost::asio::buffer(mInputData, NS_BUFSIZE),
			   boost::bind(&NetworkSession::handleReceive, this,
				       boost::asio::placeholders::error, 
				       boost::asio::placeholders::bytes_transferred));
}

void					NetworkSession::handleSend(const boost::system::error_code &)
{

}
 
void					NetworkSession::send(std::string)
{
  ;
}

boost::asio::ip::tcp::socket &		NetworkSession::getSocket()
{
  return (this->mSocket);
}
